package net.crawwle.career.crawler;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.crawwle.career.crawler.util.CrawwleConstants;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import com.couchbase.client.CouchbaseClient;
import com.google.gson.Gson;

public class test {

	public Element getTableContent(Element element, int rows, int cols) {
		Element resultElement = null;
		List<Element> trList = element.getAllElements(HTMLElementName.TR);
		if (rows < trList.size()) {
			Element trElement = trList.get(rows);
			List<Element> thList = trElement.getAllElements(HTMLElementName.TH);
			List<Element> tdList = trElement.getAllElements(HTMLElementName.TD);
			if (cols < thList.size()) {
				// Element tdElement=tdList.get(cols);
				System.out.println(thList.get(cols).getTextExtractor());
				resultElement = tdList.get(cols);
				resultElement.getTextExtractor().setIncludeAttributes(true)
						.toString();
			}

		}

		return resultElement;
	}

	public void getTableContent(Element element) {
		Element resultElement = null;
		List<Element> trList = element.getAllElements(HTMLElementName.TR);
		for (Element tr : trList) {
			Element trElement = tr;
			List<Element> thList = trElement.getAllElements(HTMLElementName.TH);
			List<Element> tdList = trElement.getAllElements(HTMLElementName.TD);
			if (thList == null || thList.size() == 0) {
				continue;
			}
			if (tdList == null || tdList.size() == 0) {
				continue;
			}
			System.out.println("th:" + thList.get(0).getTextExtractor());
			resultElement = tdList.get(0);

			/*
			 * TextExtractor textExtractor=new
			 * TextExtractor(resultElement.getContent()) { public boolean
			 * excludeElement(StartTag startTag) { return
			 * startTag.getName()==HTMLElementName.DIV ||
			 * "control".equalsIgnoreCase(startTag.getAttributeValue("class"));
			 * } };
			 */
			// TextExtractor c = new
			// CareerTextExtractor(resultElement.getContent());
			System.out.println("td:"
					+ resultElement.getTextExtractor()
							.setIncludeAttributes(true).toString());

		}
	}

	public static void test(String url) throws MalformedURLException,
			IOException {
		Source source = new Source(new URL(url));
		Element resultElement = null;
		CareerSiteInfo info = new CareerSiteInfo();

		List<Element> elementList = source
				.getAllElements(HTMLElementName.TABLE);

		for (Element element : elementList) {

			List<Element> trList = element.getAllElements(HTMLElementName.TR);
			for (Element tr : trList) {
				Element trElement = tr;
				List<Element> thList = trElement
						.getAllElements(HTMLElementName.TH);
				List<Element> tdList = trElement
						.getAllElements(HTMLElementName.TD);
				if (thList == null || thList.size() == 0) {
					continue;
				}
				if (tdList == null || tdList.size() == 0) {
					continue;
				}
				info.setUrl(url);
				String th = thList.get(0).getTextExtractor().toString();
				resultElement = tdList.get(0);
				String desc = resultElement.getTextExtractor()
						.setIncludeAttributes(true).toString();

				System.out.println("th:" + th);
				System.out.println("desc:" + desc);
				// 職種
				if (th.indexOf(CrawwleConstants.SHOKUSHU) >= 0) {
					info.setSyokusyu(desc);
				}
				// 勤務地
				else if (th.indexOf(CrawwleConstants.KINMUCHI) >= 0) {
					info.setKinnmuchi(desc);
				}
				// 年収、給与
				else if (th.indexOf(CrawwleConstants.NENNSYU) >= 0
						|| th.indexOf(CrawwleConstants.KYUUYO) >= 0) {
					info.setNensyu(desc);
				}
				// 仕事内容
				else if (th.indexOf(CrawwleConstants.WORK_DESC) >= 0) {
					info.setWorkDesc(desc);
				}
				// 雇用形態
				else if (th.indexOf(CrawwleConstants.KOYOU_KEITAI) >= 0) {
					info.setKoyouKeitai(desc);
				}
				// 勤務時間
				else if (th.indexOf(CrawwleConstants.WORK_TIME) >= 0) {
					info.setWorkTime(desc);
				} else {
					// return ;
				}

			}
		}

		System.out.println("th:" + info);

		// return info;

		System.out.println("=====CouchBase========");

		Gson gson = new Gson();

		ArrayList<URI> nodes = new ArrayList<URI>();
		nodes.add(URI.create("http://127.0.0.1:8091/pools"));
		try {
			CouchbaseClient client = new CouchbaseClient(nodes, "career", "");
			client.set(url, gson.toJson(info)).get();
			// Return the result and cast it to string
			String result = (String) client.get(url);
			System.out.println(result);

			// Shutdown the client
			client.shutdown();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		System.out.println("=============");
	}

	public static void test2(String url) throws MalformedURLException,
			IOException {
		Source source = new Source(new URL(url));
		Element resultElement = null;
		CareerSiteInfo info = new CareerSiteInfo();

		List<Element> elementList = source.getAllElements(HTMLElementName.LI);

		for (Element element : elementList) {
			if (element.toString().indexOf("JsRqmtInfo") < 0) {
				continue;
			}

			List<Element> herfList = element.getAllElements(HTMLElementName.A);
			if (herfList == null || herfList.size() == 0) {
				continue;
			}
			info.setCompanyName(herfList.get(0).getTextExtractor().toString());
			List<Element> pList = element.getAllElements(HTMLElementName.P);
			if (pList == null || pList.size() == 0) {
				continue;
			}
			StringBuffer desc = new StringBuffer();
			for (Element p : pList) {
				desc.append(p.getTextExtractor() + "\n");
			}
			info.setWorkDesc(desc.toString());
		}

		System.out.println("th:" + info);

		// return info;
	}

	public static void test3(String url) throws Exception {
		Source source = new Source(new URL(url));

		ArrayList<URI> nodes = new ArrayList<URI>();
		nodes.add(URI.create("http://127.0.0.1:8091/pools"));
		CouchbaseClient client = new CouchbaseClient(nodes, "career", "");

		CareerSiteInfo info;

		List<Element> elementList = source.getAllElements(HTMLElementName.LI);

		for (Element element : elementList) {
			if (element.toString().indexOf("JsRqmtInfo") < 0) {
				continue;
			}

			List<Element> herfList = element.getAllElements(HTMLElementName.A);
			if (herfList == null || herfList.size() == 0) {
				continue;
			}

			info = new CareerSiteInfo();
			String aText = herfList.get(0).getTextExtractor().toString();
			if (aText == null || "".equals(aText)) {
				continue;
			}
			String[] tList = aText.split(" ", 2);

			info.setCompanyName(tList[0]);

			if (tList.length > 1) {
				info.setSyokusyu(tList[1]);
			}
			info.setUrl(herfList.get(0).getAttributeValue("href"));
			List<Element> liList = element.getAllElements(HTMLElementName.LI);
			for (Element li : liList) {
				// li.get
				// List<Element> h2List = li.getAllElements(HTMLElementName.H2);
				List<Element> h3List = li.getAllElements(HTMLElementName.H3);
				if (h3List == null || h3List.size() == 0) {
					continue;
				}
				String h3 = h3List.get(0).getTextExtractor().toString();
				List<Element> pList = li.getAllElements(HTMLElementName.P);
				if (pList == null || pList.size() == 0) {
					continue;
				}
				StringBuffer descBuf = new StringBuffer();
				for (Element p : pList) {
					descBuf.append(p.getTextExtractor());

				}
				String desc = descBuf.toString();
				// 職種
				if (h3.indexOf(CrawwleConstants.SHOKUSHU) >= 0) {
					info.setSyokusyu(desc);
				}
				// 勤務地
				else if (h3.indexOf(CrawwleConstants.KINMUCHI) >= 0) {
					info.setKinnmuchi(desc);
				}
				// 年収、給与
				else if (h3.indexOf(CrawwleConstants.NENNSYU) >= 0
						|| h3.indexOf(CrawwleConstants.KYUUYO) >= 0) {
					info.setNensyu(desc);
				}
				// 仕事内容 、概要
				else if (h3.indexOf(CrawwleConstants.WORK_DESC) >= 0
						|| h3.indexOf(CrawwleConstants.WORK_GAIYOU1) >= 0) {
					info.setWorkDesc(desc);
				}
				// 雇用形態
				else if (h3.indexOf(CrawwleConstants.KOYOU_KEITAI) >= 0) {
					info.setKoyouKeitai(desc);
				}
				// 勤務時間
				else if (h3.indexOf(CrawwleConstants.WORK_TIME) >= 0) {
					info.setWorkTime(desc);
				} else {
					// return ;
				}
			}

			if (info.getCompanyName() == null) {
				continue;
			}

			// return info;

			System.out.println("=====CouchBase========");

			Gson gson = new Gson();
			client.set(info.getUrl(), gson.toJson(info)).get();
			// Return the result and cast it to string
			String result = (String) client.get(info.getUrl());
			System.out.println(result);

			// Shutdown the client
			System.out.println("=============");

		}

		client.shutdown();
		// return info;
	}

	public static void test4(String url) throws Exception {
		Source source = new Source(new URL(url));

		ArrayList<URI> nodes = new ArrayList<URI>();
		nodes.add(URI.create("http://127.0.0.1:8091/pools"));
		CouchbaseClient client = new CouchbaseClient(nodes, "career", "");

		CareerSiteInfo info;

		List<Element> elementList = source.getAllElements(HTMLElementName.DIV);

		for (Element element : elementList) {
			String style = element.getAttributeValue("class");
			if (style == null || style.indexOf("searchListDataBase") < 0) {
				continue;
			}

			List<Element> herfList = element.getAllElements(HTMLElementName.A);
			if (herfList == null || herfList.size() == 0) {
				continue;
			}

			info = new CareerSiteInfo();
			info.setCompanyName(herfList.get(0).getTextExtractor().toString());
			info.setUrl(herfList.get(0).getAttributeValue("href"));

			StringBuffer strBuf = new StringBuffer();

			if (info.getCompanyName() == null) {
				continue;
			}

			if (info.getCompanyName() == null) {
				continue;
			}
			String desc = element.getTextExtractor()
					.setIncludeAttributes(false).toString();
			if (desc == null || desc.length() == 0) {
				continue;
			}

			desc = desc.replace(info.getCompanyName(), "");
			if (desc == null || desc.length() == 0) {
				continue;
			}
			info.setWorkDesc(desc);

			// return info;

			System.out.println("=====CompanyName========"
					+ info.getCompanyName());
			System.out.println("=====desc========" + info.getWorkDesc());
			System.out.println("=====勤務地c========" + info.getKinnmuchi());
			System.out.println("=====職種========" + info.getSyokusyu());

		}

		client.shutdown();
		// return info;
	}

	public static void test5(String url, String domain) throws Exception {

		Source source = new Source(new URL(url));

		CareerSiteInfo info;

		List<Element> elementList = source.getAllElements(HTMLElementName.LI);

		for (Element element : elementList) {
			String style = element.getAttributeValue("class");
			if (style == null || (style.indexOf("resltbox") < 0
					&& style.indexOf("searchListDataBase") < 0 && style.indexOf("JsRqmtInfo") < 0 && style.indexOf("listFrame") < 0)) {
				continue;
			}

			List<Element> herfList = element.getAllElements(HTMLElementName.A);
			if (herfList == null || herfList.size() == 0) {
				continue;
			}

			info = new CareerSiteInfo();
			info.setCompanyName(herfList.get(0).getTextExtractor().toString());
			String link = herfList.get(0).getAttributeValue("href");
			if (link.indexOf(domain) >= 0) {
				info.setUrl(link);
			} else {
				info.setUrl("http://" + domain + link);
			}

			if (info.getCompanyName() == null) {
				continue;
			}
			String desc = element.getTextExtractor()
					.setIncludeAttributes(false).toString();
			if (desc == null || desc.length() == 0) {
				continue;
			}

			desc = desc.replace(info.getCompanyName(), "");
			if (desc == null || desc.length() == 0) {
				continue;
			}
			info.setWorkDesc(desc);

			// return info;

			System.out.println("=====CompanyName========"
					+ info.getCompanyName());

			System.out.println("=====url========"
					+ info.getUrl());
			System.out.println("=====desc========" + info.getWorkDesc());

			// couchbaseClient.set(info.getUrl(), gson.toJson(info)).get();
			// Return the result and cast it to string
			// String result = (String) couchbaseClient.get(info.getUrl());
			// System.out.println(result);

			// Shutdown the client
			// System.out.println("=============");

		}

	}

	public static void main(String[] args)  {
		System.out.println("http://employment.en-japan.com/special_3376/".lastIndexOf("/"));
		System.out.println("http://employment.en-japan.com".lastIndexOf("/"));
		System.out.println("http://employment.en-japan.com/".lastIndexOf("/"));
		System.setProperty("proxySet", "true");
		System.setProperty("proxyHost", "10.221.237.10");
		System.setProperty("proxyPort", "8080");
		// test("http://employment.en-japan.com/desc_530159/");
		// test3("http://next.rikunabi.com/eigyo/lst_jb0100000000/new/");
		//test5("http://employment.en-japan.com/special_3376/","");

		//test5("http://www.workport.co.jp/search/","http://www.workport.co.jp/search/");
		try {
			test5("http://next.rikunabi.com/tech_denki/lst_jb0606050000/","next.rikunabi.com");
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		//test5("http://www.hatalike.jp/PLA_001/JT_01/?SCA=01&st=P00&FF=1","www.hatalike.jp");


		//Source source = new Source(new URL("http://www.hatalike.jp/PLA_001/JT_01/?SCA=01&st=P00&FF=1"));
		//System.out.println(source.toString());



	}

}